package com.sf.hackathons.app.presenter.impl;


import com.sf.hackathons.app.helper.LoginHelper;
import com.sf.hackathons.app.presenter.constract.LoginConstract;
import com.sf.hackathons.lib.bean.UserInfo;
import com.sf.hackathons.lib.commui.base.BasePresenterImpl;
import com.sf.hackathons.lib.http.ICallBack;

/**
 * Created by guofanghua on 17/9/24.
 * 登录presenter
 */
public class LoginPresenter extends BasePresenterImpl<LoginConstract.View> implements LoginConstract.Presenter {


    @Override
    public void login(String userName, String pwd) {
        getView().showProgress();
        //http请求
        LoginHelper mLoginHelper = new LoginHelper();
        mLoginHelper.mUserName = userName;
        mLoginHelper.mMykey = pwd;
        mLoginHelper.setCallback(new ICallBack() {
            @Override
            public void onSuccess(int taskId, String result) {
                //存储token
                try {
                    UserInfo.loginSuccess(result);
                    getView().dismissProgress();
                    getView().loginSuccess();
                } catch (Exception e) {
                    e.printStackTrace();
                    getView().dismissProgress();
                    getView().loginFail("-1", "解析数据异常");
                }


            }

            @Override
            public void onFailed(int taskId, String errorCode, String msg) {
                getView().dismissProgress();
                getView().loginFail(errorCode, msg);

            }
        });
        mLoginHelper.sendHttpRequest();

    }

}